<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Frontend.php 130 2011-03-18 03:10:02Z cheng.wei $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Adapter for authentication
 * 
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: Frontend.php 130 2011-03-18 03:10:02Z cheng.wei $$
 */
class Evebit_Auth_Adapter_ClientLogin extends Evebit_Auth_Adapter_Default {
	
	const FAILURE_NEED_CAPTCHA = - 12;
	/**
	 * Performs an authentication attempt
	 *
	 * @throws Zend_Auth_Adapter_Exception If authentication cannot
	 * be performed
	 * @return Zend_Auth_Result
	 */
	public function authenticate() {
		if ($this->isGoogleLogin) {
			try {
				// get default client
				$client = Zend_Auth::getInstance ()->getStorage ()->getHttpClient ();
				
				// login by google
				Zend_Gdata_ClientLogin::getHttpClient ( $this->getIdentity (), $this->getPassword (), Zend_Gdata_Calendar::AUTH_SERVICE_NAME, $client, 'china-wanderer', $this->getToken (), $this->getCheckCode () );
			} catch ( Zend_Gdata_App_CaptchaRequiredException $e ) {
				return new Zend_Auth_Result ( self::FAILURE_NEED_CAPTCHA, array ('token' => $e->getCaptchaToken (), 'url' => $e->getCaptchaUrl () ), array ($e->getMessage () ) );
			} catch ( Zend_Gdata_App_AuthException $ae ) {
				return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, array ('token' => $this->getCaptchaToken (), 'url' => $this->getCaptchaUrl () ), array ($ae->getMessage () ) );
			
			} catch ( Zend_Gdata_App_Exception $ze ) {
				return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array ($ze->getMessage () ) );
			
			} catch ( Zend_Gdata_App_HttpException $he ) {
				return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array ($he->getMessage () ) );
			
			} catch ( Exception $e ) {
				return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array ($e->getMessage () ) );
			}
		}
		// load from table
		$user = new User_Model_Entity_User ();
		$user->load ( $this->getIdentity (), $this->identityField );
		if (! $this->isGoogleLogin) {
			$orgi_password = $user->getData ( 'password' );
			if ($orgi_password == $this->password) {
			} else {
				$password = explode ( ':', $orgi_password );
				if (md5 ( $this->password . $password [1] ) != $password [0]) {
					return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null, array (Evebit_Application::getSession ()->translate ( "The username or password error" ) ) );
				}
			}
		
		}
		
		$acl = Zend_Registry::get ( "Acl" );
		// check user have disabled
		if (! $user->getId ()) {
			return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array (Evebit_Application::getSession ()->translate ( "Your account have been setup" ) ) );
		}
		// check user have disabled
		if ($user->getUserEnable () == 2) {
			return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array (Evebit_Application::getSession ()->translate ( "Your account have been disabled" ) ) );
		}
		if ($user->getState () == 'confirming') {
			return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array (Evebit_Application::getSession ()->translate ( "Your account have been disabled, please confirm your registration" ) ) );
		}
		
		if ($this->getToragetype ()) {
			// backgound
			if (! $acl->isAllowed ( $user->getRole (), "backend" )) {
				return new Zend_Auth_Result ( Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null, array (Evebit_Application::getSession ()->translate ( 'Login fail,you do not have permission to log in the backend.' ) ) );
			}
		}
		
		// give result
		$identity = $user->getData ();
		if ($this->isGoogleLogin) {
			$identity ['token'] = $client->getClientLoginToken ();
		}
		return new Zend_Auth_Result ( Zend_Auth_Result::SUCCESS, $identity, array ("Not support" ) );
	}
}